/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package boundaries;

import utils.ImageLoader;
import controllers.CalendarManager;
import controllers.UserLoader;
import entities.Event;
import entities.OutdoorEvent;
import entities.User;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.DefaultScheduleEvent;
import org.primefaces.model.DefaultScheduleModel;
import org.primefaces.model.ScheduleEvent;
import org.primefaces.model.ScheduleModel;

/**
 *
 * @author Alberto, Alessandra
 */
@ManagedBean
@ViewScoped
public class UserCalendarBean implements Serializable {
    
    private static final Logger logger = Logger.getLogger("CalendarBean");
    
    @EJB
    private UserLoader ul;
    
    private User user;
    
    private ScheduleModel eventModel;
 
    private ScheduleEvent event = new DefaultScheduleEvent();
 
    @PostConstruct
    public void init() {
        //log info
        logger.log(Level.INFO, "Calendar bean created");
        
        eventModel = new DefaultScheduleModel();
        
        this.user = (User) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("selecteduser");
        
        ArrayList<Event> events =  ul.loadCalendar(user);
        Iterator<Event> iterevents = events.iterator();
        while(iterevents.hasNext()){
            Event e = iterevents.next();
            DefaultScheduleEvent se;
            if (e.getVisibility()) {
                se = new DefaultScheduleEvent(e.getName(), e.getStartTime(), e.getEndTime());
                se.setData(e);
            }
            else 
                se = new DefaultScheduleEvent("Busy Time Slot", e.getStartTime(), e.getEndTime());
            
            eventModel.addEvent(se);
        }
    }
     
    public ScheduleModel getEventModel() {
        return eventModel;
    }
     
    public ScheduleEvent getEvent() {
        return event;
    }
 
    public void setEvent(ScheduleEvent event) {
        this.event = event;
    }
    
    public void addEvent(ActionEvent actionEvent) {
        if(event.getId() == null)
            eventModel.addEvent(event);
        else
            eventModel.updateEvent(event);
         
        event = new DefaultScheduleEvent();
    }
     
    public void onEventSelect(SelectEvent selectEvent) {
        event = (ScheduleEvent) selectEvent.getObject();
    }
     
    public String getType () {
        if (event.getData() instanceof OutdoorEvent)
            return "Outdoor";
        else return "Indoor";
    }
    
    public String getWeatherForecast () {
        if (event.getData() instanceof OutdoorEvent && ((OutdoorEvent)event.getData()).isForecasted()) {
            return ImageLoader.getWeatherImg(((OutdoorEvent)event.getData()).getWeatherforecast().getName());
        } else {
            return "";
        }
    }
    
    public String getUsername () {
        return this.user.getUsername();
    }
}
